6.2.11 共享镜像层

多个镜像之间可以并且确实会共享镜像层。这样可以有效节省空间并提升性能。

请再回顾一下之前用于拉取 nigelpoulton/tu-demo 仓库下全部包含标签的 docker image pull 命令(包含 -a 参数)。

$ docker image pull -a nigelpoulton/tu-demo
latest: Pulling from nigelpoulton/tu-demo
237d5fcd25cf: Pull complete
a3ed95caeb02: Pull complete
<Snip>
Digest: sha256:42e34e546cee61adb100...a0c5b53f324a9e1c1aae451e9
v1: Pulling from nigelpoulton/tu-demo
237d5fcd25cf: Already exists
a3ed95caeb02: Already exists
<Snip>
Digest: sha256:9ccc0c67e5c5eaae4beb...24c1d5c80f2c9623cbcc9b59a
v2: Pulling from nigelpoulton/tu-demo
237d5fcd25cf: Already exists
a3ed95caeb02: Already exists
<Snip>
eab5aaac65de: Pull complete
Digest: sha256:d3c0d8c9d5719d31b79c...fef58a7e038cf0ef2ba5eb74c
Status: Downloaded newer image for nigelpoulton/tu-demo
$ docker image ls
REPOSITORY             TAG      IMAGE ID       CREATED        SIZE
nigelpoulton/tu-demo   v2       6ac...ead   4 months ago   211.6 MB
nigelpoulton/tu-demo   latest   9b9...e29   4 months ago   211.6 MB
nigelpoulton/tu-demo   v1       9b9...e29   4 months ago   211.6 MB

注意那些以 Already exists 结尾的行。

由这几行可见,Docker很聪明,可以识别出要拉取的镜像中,哪几层已经在本地存在。在本例中,Docker首先尝试拉取标签为 latest 的镜像。然后,当拉取标签为 v1v2 的镜像时,Docker注意到组成这两个镜像的镜像层,有一部分已经存在了。出现这种情况的原因是前面3个镜像相似度很高,所以共享了很多镜像层。

如前所述,Docker在Linux上支持很多存储引擎(Snapshotter)。每个存储引擎都有自己的镜像分层、镜像层共享以及写时复制(CoW)技术的具体实现。但是,其最终效果和用户体验是完全一致的。尽管Windows只支持一种存储引擎,还是可以提供与Linux相同的功能体验。

results matching ""

    No results matching ""